package net.koodar.forge.system.role.manager;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.koodar.forge.system.role.model.entity.Role;
import net.koodar.forge.system.role.dao.RoleMapper;
import net.koodar.forge.common.exception.BizException;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author liyc
 */
@Service
@RequiredArgsConstructor
public class RoleManager extends ServiceImpl<RoleMapper, Role> {

	private final RoleMapper roleMapper;

	public void deleteRole(Long id) {
		baseMapper.deleteById(id);
	}

	public Role findById(Long id) {
		Role role = roleMapper.selectById(id);
		if (role == null) {
			throw new BizException(String.format("角色[%s]不存在", id));
		}
		return role;
	}

	public Role findByCode(String code) {
		List<Role> roles = roleMapper.selectList(Wrappers.<Role>lambdaQuery().eq(Role::getCode, code));
		return roles.isEmpty() ? null : roles.get(0);
	}

	public List<Role> findByUserId(Long userId) {
		return roleMapper.findByUserId(userId);
	}
}
